package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author xiaoping
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    /**
     * 在支付宝创建的应用的id
     */
    private  String app_id = "2016102300745063";

    /**
     *  商户私钥，您的PKCS8格式RSA2私钥
     */
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCckRr66FuKlbAxkcyTjOsfTfh1ulwpGMltk3fc/e6RLZsAf42nuG2C9YPSCvO2GdMbrtn8H1euXo9ywxs7QMZWh7p+ooHPUi97MvNhpbivIEQnLUP7wqVmWjSbYyqVPkZbHmjeofwZv1N3x2IARybDpG3PgW6yiERgLa2mfH3MbCO55kVjnfmOggPc0zebI2MpNpkMUAhGjMDnsrqODquehw3knjKH20oS4AE6pAP7hacTyb1BUe5ln6mwIZ0E8X9By4kUaI6GEmtAnmMSyOgg0tIaDs8NW0nRsJLyH5gSTPbssTeg5QNn3AlVZy6PCg5vu0EUEEcywiJrLqV1D46vAgMBAAECggEAaMwrzgSXuYSGYjegQqdRfiu3gj3SkGOZ+DF8Srzh4eUv2j6Gt9wX2U+zA3X3OU3B48a+/CaSjxGWXQ/Be4DZAXRqXXczAm2VgJZTGGpP21DyrXJ8Ddl00rTQf636A9miSD1rnpG4aBeLrCIWM+9sQAogwoN1cCUL94C7ujOcFKbEN4RPpxRB0HzQw005wrtDmMCmeQM/LXE6fN5uVPDw8V+j5ofAK+SbOYjkaD2ctN8CoZURokMjRLl0LdSnJpdt9kK3YWgOwAbu8llbmrAo1UUql6qBmBt60AmsVYgvFrWcD/OWk3oEYBPsDEWqR/B9X3XL5kITPuKfmD0jnKmWWQKBgQDd6a4xMpIFNqNgn+4urzIZN/V2lYOdxBqo2rxfAJgP+JVUb0qEviU+hDiCCbYN9bwoSIpA1Bv4Kbgsxs6U/PgTUPTbwwddHXWN13F3L8dlarN4CCJjehs+OyjJ+YZy37o6F4bDJ7iJVgyPDfkiE6xGddZuvxWW1w136sL9mTazBQKBgQC0nc/eG9cANvyq1d5/lOgtXUGtERlpq8dHpCVGxl9mTRcnYAE7a2lPKhc5V2YvYstTJnO0JqO4CV+c8U2KO5U6oUBjolNm7DHjenuVLwTUAH0EgBXzxhvjER7lSQlCbgfCRLU7y6Li804M0Lyc+qoZp5QdrYy3OisDgStIGBbRIwKBgBM5A9LVK5ryAbMEB5DZskBG/adZCj6nVxvbXsfn+EQh52WaAl1/TyCcOXziNHf/Gl5u8jLtbMLRclfGnOyBAUhDEgLbYQ7q110DFZwacm5RkNxL8Ban5CY+36inScM3qB0SHqVXEoArCQl3T2ZpR9oOqRWMzeT0GgGSP3PSSs+1AoGAZFMganxGUa05bd88Jtvcl44sc+iCmCq9l7K6rfR0e0uo58sxBANmzH1Rxx0f+oRC8l3yeQfZetliDbgPXRA5lQaK0I0TDKF4GKYpTTV/1g6rYruO9gh95wPUiIGoLgkWMDef/6XeRXfmS8IKcpvdrkBiSpXjCse5qsmt8+7FhccCgYBO4pzADfgcf4JmRmo65/n/gD9C/D5aSNoNY6YiAdbd4wGzJKWM0TfP8LwuTFNehfd+okdWRROh8nVfg19UQBscWAz6yKSLxZ/NOp6sJFosLaDTquBPYmoV0UFyblVOLrNjo6tHWkGPhDpacYMF3HeBS8S2xBqzc5coNDDTI/67Bw==";
    /**
     * 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
     */
    private  String alipay_public_key ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyhZTLmjjjP1sZ1SmAtOdhQLll4t7vmXDlHQBg9NODcUwK3nTAgz03c5Ch2rujaV2FfllTW9ZY/iPvtI9J0mFq7GE/TYIzOAmAaSm0KxA5h8FWVy/YkZgG0MeBi1utfbvOY0+cFGc7BlMDq7+x4YIeLQXFh6gSdzNc2fIQv1OrLxbvbB01QogX4WtPYtMKP7qvaO5IjTZ3v5ka/RcZA4lYA1sqRP0AlHxL/dM9ukMKdGiQn8lQ8JaSkEAxZ6S5WjXo1vyzaeyvRn58M9HYX98wMAo99AvvJEsV2zfYz9RFNWXdyuI1q7EtHCuUiNwk2XIOO8dF5YVIleQ0FfszF6O4wIDAQAB";
    /**
     * 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
     *  支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
     */
    private  String notify_url="http://3jnuylzi0g.54http.tech/payed/notify";

    /**
     * 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
     *    同步通知，支付成功，一般跳转到成功页
     */
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 超时时间
    private  String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\"," //1分钟
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }

}
